package com.chunxuan.langquan.ui.activity.store;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import io.reactivex.functions.Consumer;

/**
 * 说明：$
 * <p>
 * date: 2021/3/3 10:30 PM
 *
 * @author syd
 * @version 1.0
 */
public class DeleteEnsureDialog extends DialogFragment {
    private TextView tvDelete;
    private TextView tvCancel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE,
                android.R.style.Theme_Material_Light_Dialog_MinWidth);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_shoppingcar_tip_delete, container, false);
        tvDelete = view.findViewById(R.id.bt_ensure);
        tvCancel = view.findViewById(R.id.bt_cancel);
        tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = getArguments().getString("id");
                requestDelete(id);
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        return view;
    }

    @SuppressLint("CheckResult")
    public void requestDelete(String id) {
        APIRetrofit.getDefault().deleteProduct(Global.HEADER, id)
                .compose(RxUtil.<BaseResult2<String>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<String>>() {
                    @Override
                    public void accept(BaseResult2<String> result2) throws Exception {
                        if (result2.isSuccess()) {
                            ((ShoppingCarActivity) getActivity()).requestCarProducts();
                            dismiss();
                        } else {
                            ToastUtils.showShort(result2.getMsg());
                        }
//                        Logg.i("reqUserInfo.result2.getMsg()" + result2.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        throwable.printStackTrace();
                        ToastUtils.showShort("删除失败");
                    }
                });
    }
}
